package com.bnyy.gbp.customsView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ScreenUtils;
import com.bnyy.gbp.R;

@SuppressLint("AppCompatCustomView")
public class ScaleImageView extends ImageView {

    float mScale, mImageWidthMixOfScreenWidth;
    float height, width;

    public ScaleImageView(Context context) {
        super(context);
    }

    public ScaleImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.ScaleImageView);
        mScale = attr.getFloat(R.styleable.ScaleImageView_imageScale, -1);
        mImageWidthMixOfScreenWidth = attr.getFloat(R.styleable.ScaleImageView_imageWidthMixOfScreenWidth, 1);
        height = attr.getInt(R.styleable.ScaleImageView_imageHeight, -1);
        width = attr.getInt(R.styleable.ScaleImageView_imageWidth, -1);
        attr.recycle();

        if (mScale < 0) {
            if (height > 0 && width > 0) {
                mScale = height / width;
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mScale > 0 && mImageWidthMixOfScreenWidth > 0 && mImageWidthMixOfScreenWidth <= 1) {
            float imageWidth = (ScreenUtils.getScreenWidth() * mImageWidthMixOfScreenWidth) + 1;
            float imageHeight = (imageWidth * mScale) + 1;
            setMeasuredDimension((int) imageWidth, (int) imageHeight);
        }
    }

    public void setSize(float mScale, float mImageWidthMixOfScreenWidth) {
        this.mScale = mScale;
        this.mImageWidthMixOfScreenWidth = mImageWidthMixOfScreenWidth;
        requestLayout();
    }
}























